package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.InvInStockEntity;
import com.yvtian.inv.dao.InvInStockMapper;
import com.yvtian.inv.pojo.InvMaterialReturnEntity;
import com.yvtian.inv.pojo.InvOrderEntity;
import com.yvtian.inv.pojo.ShopEntity;
import com.yvtian.inv.service.InvInStockService;
import com.yvtian.inv.service.InvOrderService;
import com.yvtian.inv.service.ShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 生产入库单(inv_in_stock)表服务实现类
 *
 * @author long
 * @since 2024-07-02 13:43:35
 */
@Service("invInStockService")
public class InvInStockServiceImpl extends ServiceImpl<InvInStockMapper, InvInStockEntity> implements InvInStockService {

    @Autowired
    private ShopService shopService;
    @Autowired
    private InvOrderService invOrderService;

    @Override
    public IPage<InvInStockEntity> findByPage(Page<InvInStockEntity> page, InvInStockEntity invInStockEntity) {
        LambdaQueryWrapper<InvInStockEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvInStockEntity::getIsDel, 0);
        getQeuryWrapper(invInStockEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean handleStorage(InvInStockEntity invInStockEntity) {
        BigDecimal accQuantity = invInStockEntity.getAccQuantity();
        String shopCode = invInStockEntity.getShopCode();

        //更新库存
        shopService.update(Wrappers.<ShopEntity>lambdaUpdate()
                .eq(ShopEntity::getShopCode, shopCode)
                .setSql("quantity = quantity + " + accQuantity.doubleValue()));

        //更新订单表状态(生产入库)
        invOrderService.update(Wrappers.<InvOrderEntity>lambdaUpdate()
                .eq(InvOrderEntity::getCode,invInStockEntity.getInvOrderCode())
                .set(InvOrderEntity::getProductionStatus,7));

        //更新主表状态（审核）
        LambdaUpdateWrapper<InvInStockEntity> invInStockUpdateWrapper = new LambdaUpdateWrapper<>();
        invInStockUpdateWrapper.eq(InvInStockEntity::getCode, invInStockEntity.getCode());
        invInStockUpdateWrapper.set(InvInStockEntity::getStatus, 1);
        invInStockUpdateWrapper.set(InvInStockEntity::getCheckName, invInStockEntity.getCheckName());
        invInStockUpdateWrapper.set(InvInStockEntity::getCheckUser, invInStockEntity.getCheckUser());
        invInStockUpdateWrapper.set(InvInStockEntity::getCheckTime, new Date());
        this.update(invInStockUpdateWrapper);
        return true;
    }

    private void getQeuryWrapper(InvInStockEntity invInStockEntity, LambdaQueryWrapper<InvInStockEntity> queryWrapper) {
        if (StringHelper.isNotEmpty(invInStockEntity.getCode())) {
            queryWrapper.like(InvInStockEntity::getCode, invInStockEntity.getCode());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getInvOrderCode())) {
            queryWrapper.like(InvInStockEntity::getInvOrderCode, invInStockEntity.getInvOrderCode());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getStockCode())) {
            queryWrapper.like(InvInStockEntity::getStockCode, invInStockEntity.getStockCode());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getStockName())) {
            queryWrapper.like(InvInStockEntity::getStockName, invInStockEntity.getStockName());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getStockLocation())) {
            queryWrapper.like(InvInStockEntity::getStockLocation, invInStockEntity.getStockLocation());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getProjectCode())) {
            queryWrapper.like(InvInStockEntity::getProjectCode, invInStockEntity.getProjectCode());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getProjectName())) {
            queryWrapper.like(InvInStockEntity::getProjectName, invInStockEntity.getProjectName());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getShopCode())) {
            queryWrapper.like(InvInStockEntity::getShopCode, invInStockEntity.getShopCode());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getShopName())) {
            queryWrapper.like(InvInStockEntity::getShopName, invInStockEntity.getShopName());
        }
        if (invInStockEntity.getAccQuantity() != null) {
            queryWrapper.eq(InvInStockEntity::getAccQuantity, invInStockEntity.getAccQuantity());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getAttribute())) {
            queryWrapper.like(InvInStockEntity::getAttribute, invInStockEntity.getAttribute());
        }
        if (invInStockEntity.getUnit() != null) {
            queryWrapper.eq(InvInStockEntity::getUnit, invInStockEntity.getUnit());
        }
        if (invInStockEntity.getPrice() != null) {
            queryWrapper.eq(InvInStockEntity::getPrice, invInStockEntity.getPrice());
        }
        if (invInStockEntity.getAmount() != null) {
            queryWrapper.eq(InvInStockEntity::getAmount, invInStockEntity.getAmount());
        }
        if (invInStockEntity.getIsInbound() != null) {
            queryWrapper.eq(InvInStockEntity::getIsInbound, invInStockEntity.getIsInbound());
        }
        if (ObjectUtils.isNotEmpty(invInStockEntity.getStatus())) {
            queryWrapper.eq(InvInStockEntity::getStatus, invInStockEntity.getStatus());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getCreateName())) {
            queryWrapper.like(InvInStockEntity::getCreateName, invInStockEntity.getCreateName());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getUpdateName())) {
            queryWrapper.like(InvInStockEntity::getUpdateName, invInStockEntity.getUpdateName());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getCreateUser())) {
            queryWrapper.like(InvInStockEntity::getCreateUser, invInStockEntity.getCreateUser());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getRemarks())) {
            queryWrapper.like(InvInStockEntity::getRemarks, invInStockEntity.getRemarks());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getCheckName())) {
            queryWrapper.like(InvInStockEntity::getCheckName, invInStockEntity.getCheckName());
        }
        if (StringHelper.isNotEmpty(invInStockEntity.getCheckUser())) {
            queryWrapper.like(InvInStockEntity::getCheckUser, invInStockEntity.getCheckUser());
        }
    }
}

